##############################################################################
####State Capacity, Insurgency, and Civil War - Andhra Pradesh Validation ####
##############################################################################
library(MASS) 
library(pscl)
library(foreign)
library(Hmisc)
library(rgdal)
library(RArcInfo)
library(stargazer)
library(mvtnorm)
library(Zelig)
library(readstata13)
library(pROC)
library(robust)
library(cvTools)
library(boot)
library(maps)
library(mapdata)
library(doBy)
library(raster)
library(survival)
library(spduration)
library(DAMisc)
library(plyr)
library(DataCombine)
library(plm)
library(AER)
library(lmtest)

########################
###Create The Dataset###
########################
###Importing the main data
#set working directory to location of dta file
setwd("~/Google Drive/NSLC/Final Dofiles/")
#Import the data
AP.dat <- read.dta("APdat.dta")

##########################
###CW Onset replication###
##########################
###Baseline
#No NTL
FL.ap.b <- glm(civconf ~ lagcivconflagtemp + logPHS + logppp + logpop + year + cluster(gid),
               data=AP.dat, family = "binomial")
summary(FL.ap.b)
AIC(FL.ap.b)
#With NTL
FL.ap.b.nl <- glm(civconf ~ lagcivconflagtemp + lnNL_sum + logppp + logpop + year + cluster(gid),
               data=AP.dat, family = "binomial")
summary(FL.ap.b.nl)
AIC(FL.ap.b.nl)

###Full
#No NTL
FL.ap.f <- glm(civconf ~ lagcivconflagtemp + logPHS + logppp + logpop + mnt1 + logbdist1 +  
                 logttime + year + cluster(gid),
               data=AP.dat, family = "binomial")
summary(FL.ap.f)
AIC(FL.ap.f)

#NTL
FL.ap.f.nl <- glm(civconf ~ lagcivconflagtemp + lnNL_sum + logppp + logpop + mnt1 + logbdist1 +  
                 logttime + year + cluster(gid),
               data=AP.dat, family = "binomial")
summary(FL.ap.f.nl)
AIC(FL.ap.f.nl)

##Export to LaTex
stargazer(FL.ap.b, FL.ap.b.nl, FL.ap.f, FL.ap.f.nl)